home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Examples / FileBrowser / Inspector.subproj / MiscClipTextField.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  1.8 KB  |  57 lines

  1. //        Written by Don Yacktman Copyright (c) 1995 by Don Yacktman.
  2. //                Version 1.0.  All rights reserved.
  3. //
  4. //        This notice may not be removed from this source code.
  5. //
  6. //    This object is included in the MiscKit by permission from the author
  7. //    and its use is governed by the MiscKit license, found in the file
  8. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9. //    for a list of all applicable permissions and restrictions.
  10. //    
  11.  
  12. #import "MiscClipTextField.h"
  13. #import <misckit/misckit.h>
  14.  
  15. @implementation MiscClipTextField : TextField
  16.  
  17. #ifdef NOISYDEBUG
  18. #define PRINT printf("TextField (width %f) set to:  \"%s\"  width %f.\n", \
  19.         NX_WIDTH(&bounds), [temp stringValue], \
  20.         [[self font] getWidthOf:[temp stringValue]])
  21. #define PRINT1 printf("\n")
  22. #else
  23. #define PRINT 
  24. #define PRINT1 
  25. #endif
  26.  
  27. #define SLOP 18.0 // how many extra pixels of margin we need...
  28.         // tweaked so that the fields _looks_ good.
  29.  
  30. - setStringValue:(const char *)aString
  31. {
  32.     id temp = [MiscString new];
  33.     id mainFont = [self font];
  34.     id scrnFont = [mainFont screenFont];
  35.     id theFont = (scrnFont ? scrnFont : mainFont); // use screen font if exists
  36.  
  37.     [temp setStringValue:aString];
  38.     PRINT;
  39.     // Using ceil and floor makes sure that if we err, we do it in the
  40.     // direction of safety.  (And we will never err more than a pixel...)
  41.     // so I guess the SLOP is going to make more of a difference anyway.
  42.     if (ceil([theFont getWidthOf:[temp stringValue]]) >
  43.             floor((NX_WIDTH(&bounds) - SLOP))) { // too long?
  44.         [temp insert:"..." at:0];
  45.         while (ceil([theFont getWidthOf:[temp stringValue]]) >
  46.                 floor((NX_WIDTH(&bounds) - SLOP))) {
  47.             [temp replaceFrom:3 length:1 with:""]; // pull out chars
  48.             PRINT;
  49.         }
  50.         // now we should be down to a size that will fit.
  51.     }
  52.     PRINT1;
  53.     return [super setStringValue:[temp stringValueAndFree]];
  54. }
  55.  
  56. @end
  57.